﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace com.lover.astd.common.model
{
    public class ServerResult
    {
        private string _innerXml;
        public bool HttpSucceed
        {
            get { return _httpCode == 200; }
        }
        private int _httpCode;
        public int HttpCode
        {
            get { return _httpCode; }
            set { _httpCode = value; }
        }
        public string getHttpErrorInfo()
        {
            string retstr = "";
            switch (_httpCode)
            {
                case -1: retstr = "网断了"; break;
                case 404: retstr = "404 未找到网页"; break;
                case 500: retstr = "500 服务器错误"; break;
            }
            return retstr;
        }
        public string getDebugInfo()
        {
            if (!HttpSucceed) return getHttpErrorInfo();
            else if (!CmdSucceed) return CmdError;
            else return _innerXml;
        }
        private bool _cmdSucceed;
        public bool CmdSucceed
        {
            get { return _cmdSucceed; }
        }
        private string _cmdError;
        public string CmdError
        {
            get { return _cmdError; }
        }
        private XmlDocument _cmdResult;
        public XmlDocument CmdResult
        {
            get { return _cmdResult; }
            set { _cmdResult = value; }
        }
        public ServerResult(string server_return)
        {
            if (server_return == null || server_return == "")
            {
                _cmdSucceed = false;
                _cmdError = "";
                _cmdResult = null;
                return;
            }
            if (server_return.StartsWith("code:"))
            {
                string codestr = server_return.Replace("code:", "");
                int code = Int32.Parse(codestr);
                // 到这里来了, code肯定不是200
                _httpCode = code;
                return;
            }
            _httpCode = 200;
            _cmdResult = new XmlDocument();
            server_return = server_return.Replace("&", "");
            _cmdResult.LoadXml(server_return);
            XmlNode node = _cmdResult.SelectSingleNode("/results/state");
            if (node == null || node.InnerText != "1")
            {
                if (node.InnerText == "3") // 出现验证码了
                {
                    _cmdError = "出现验证码了";
                }
                else
                {
                    XmlNode errnode = _cmdResult.SelectSingleNode("/results/message");
                    if (errnode != null) _cmdError = errnode.InnerText;
                    else
                    {
                        errnode = _cmdResult.SelectSingleNode("/results/exception");
                        if (errnode != null) _cmdError = errnode.InnerText;
                    }
                }
                
                if (_cmdError == null) _cmdError = server_return;
                _cmdSucceed = false;
            }
            else
            {
                _innerXml = server_return;
                _cmdError = "";
                _cmdSucceed = true;
            }
        }
    }
}
